package com.czmh.etc.onlinepubsys.controller.cmg;

import com.czmh.etc.onlinepubsys.constants.cmg.BusinessType;
import com.czmh.etc.onlinepubsys.enums.cmg.ApiCodeEnum;
import com.czmh.etc.onlinepubsys.json.cmg.JSONResult;
import com.czmh.etc.onlinepubsys.request.cmg.IssueOrgChangeListRequest;
import com.czmh.etc.onlinepubsys.response.cmg.ApiResponseBase;
import com.czmh.etc.onlinepubsys.utils.cmg.CmgHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/{version}/api/cmg/ioapi")
@Api(tags = "申办发行及售后变更通知接口相关 - 招商局")
public class CmgController {
    @Value("${cmg.accessno}")
    private String accessno;

    @PostMapping("/issueOrgChangeList")
    @ApiOperation("通知发行渠道变更列表")
    public JSONResult issueOrgChangeList() {
        JSONResult result = new JSONResult();
        log.info("issueOrgChangeList:{}");
        IssueOrgChangeListRequest req = new IssueOrgChangeListRequest();
        String version = DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
        Long time = System.currentTimeMillis();
        req.setClientKey(accessno + time);
        req.setTime(time);
        req.setVersion(version);
        ApiResponseBase r = CmgHttpUtil.newpost(BusinessType.NEW_ISSUE_ORG_CHANGE_LIST, req);
        if(r != null && r.getCode().equalsIgnoreCase(ApiCodeEnum.SUCCESS.getCode())){
            if(r.bizContent != null){
                result.setRspData(r.getBizContent());
                result.setMessage(r.getMsg());
                result.setCurrentTime(r.getReceiveTime());
            }
        }else{
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r.getMsg());
            result.setCurrentTime(r.getReceiveTime());
        }
        return result;
    }

}
